package topcoder.srm325;


public class RGBStreet {

	public int estimateCost(String[] houses) {
		int N = houses.length;
		int[][] st = new int[N][3];

		for (int i = 0; i < N; i++) {
			String[] s = houses[i].split(" ");
			for (int j = 0; j < 3; j++) {
				st[i][j] = Integer.valueOf(s[j]);
			}
		}
		for (int i = 1; i < N; i++) {
			for (int j = 0; j < 3; j++) {
				st[i][j] = st[i][j]
						+ Math.min(st[i - 1][(j + 1) % 3],
								st[i - 1][(j + 2) % 3]);
			}

		}
		if (st.length == 1)
			return st[0][0] + st[0][1] + st[0][2];
		else

			return min_array(st[N - 1]);

	}

	private int min_array(int[] is) {
		int m = Integer.MAX_VALUE;
		for (int i : is) {
			if (i < m)
				m = i;
		}
		return m;

	}

	public static void main(String[] args) {
		RGBStreet rgb = new RGBStreet();
		String[] houses = { "71 39 44", "32 83 55", "51 37 63", "89 29 100",
				"83 58 11", "65 13 15", "47 25 29", "60 66 19" };
		;

		int c = rgb.estimateCost(houses);
		System.out.println(c);
	}

}
